package com.xxs.controller.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.xxs.common.Const;
import com.xxs.directive.NodeListDirective;
import com.xxs.interceptor.AdminInterceptor;
import com.xxs.utils.ftp.FTPTest_04;
import com.xxs.utils.ftp.FTPUtils;
import com.xxs.utils.xml.XmlDom4jUtils;

/**
 * 站点管理
 */
@Before(AdminInterceptor.class)
public class GenerateController extends Controller {
	//生成整站静态化
	public void build(){
		String siteCode = getPara(0);
		buildIndex(siteCode);
//		buildArticle(siteCode);
		renderText("生成成功！！！");
	}
	//远程发布到ftp服务器
	public void publish(){
		String siteCode = getPara(0);
		FTPTest_04 ftp=new FTPTest_04("bxw2713610165.my3w.com",21,"bxw2713610165","Qwertyuiop123");
        ftp.ftpLogin();
        System.out.println("FTP服务器登陆成功");
        //获取要上传的文件夹
        String webPath = PathKit.getWebRootPath();
        String staticPath = webPath+"\\db\\"+siteCode+"\\front\\html";
        System.out.println("拼接的要上传的本地文件夹地址是："+staticPath);
        //上传文件夹  
        boolean uploadFlag = ftp.uploadDirectory(staticPath, "/"); //如果是admin/那么传的就是所有文件，如果只是/那么就是传文件夹
        System.out.println("uploadFlag 结果: " + uploadFlag);
        //下载文件夹  
//        ftp.downLoadDirectory("d:\\tm", "/");
        ftp.ftpLogOut();
    	renderText("发布成功！！！");
    }
	/**
	 * 生成首页静态页面
	 * @param sitecode
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void buildIndex(String siteCode){
		System.out.println("siteCode:"+siteCode);
//		String path = getRequest().getServletPath();
    	String _files = "./_files";
		//生成首页
    	String webPath = PathKit.getWebRootPath();
    	Engine.use().setBaseTemplatePath(webPath);
//    	Engine.use().addDirective("nodeList", new NodeListDirective());
//    	Engine.use().addDirective("nodeList", new NodeListDirective());
    	List<Element> elements = XmlDom4jUtils.getElements(getXmlPath(siteCode, "node"),"node");
    	String templatePath = "/db/"+siteCode+"/front/index.html";
    	String staticPath = webPath+"/db/"+siteCode+"/front/html/index.html";
    	Template template = Engine.use().getTemplate(templatePath);
    	File file = new File(staticPath);
    	try {
    		if (!file.exists()) {
    			file.createNewFile();
    		}
			Map mm = new HashMap<>();
			mm.put("_files", _files);
			mm.put("elements", elements);
			mm.put("siteCode", siteCode);
			String str = template.renderToString(mm);
			System.out.println(template.renderToString(mm));
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(str.getBytes());
			fos.close();
			//上传到ftp
//			try {
//				FileInputStream in=new FileInputStream(file); 
//				boolean flag = FTPUtils.uploadFile("bxw2713610165.my3w.com", 21, "bxw2713610165", "Qwertyuiop123", "/ftp", "index.html", in); 
//				System.out.println(flag+"上传成功！"); 
//    		} catch (FileNotFoundException e) { 
//    			e.printStackTrace(); 
//    		} 
			System.out.println("静态化完成");
		} catch (IOException e) {
			e.printStackTrace();
		}
    	renderText("生成成功！！！");
    }
	/**
	 * 生成内容静态页面
	 * @param sitecode
	 */
	public void buildArticle(String siteCode){
		System.out.println("siteCode:"+siteCode);
		//生成首页
//    	Engine engine = new Engine();
//    	String webPath = PathKit.getWebRootPath();
//    	engine.setBaseTemplatePath(webPath);
//    	Template template = engine.getTemplate("db/"+siteCode+"/front/index.html");
//    	String res = template.renderToString(JMap.create("_files","xxxxxx"));
////    	System.out.println("ooooooo:"+webPath+"/db/"+siteCode+"/front/html/index.html");
//    	File file = new File(webPath+"/db/"+siteCode+"/front/html/index.html");
//    	try {
//    		if (!file.exists()) {
//    			file.createNewFile();
//    		}
//			Map mm = new HashMap<>();
//			mm.put("qqq", "888888");
////			template.rendert(mm,bw);
//			String str = template.renderToString(mm);
//			System.out.println(template.renderToString(mm));
//			FileOutputStream fos = new FileOutputStream(file);
//			fos.write(str.getBytes());
//			fos.close();
//			System.out.println("静态化完成");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
    	renderText("生成成功！！！");
    }
	public String getXmlPath(String siteCode,String modelName){
		if(siteCode!=null){
			return Const.XML_PREFIX+siteCode+"/"+modelName+Const.XML_SUFFIX;
		}else{
			return Const.XML_PREFIX+modelName+Const.XML_SUFFIX;
		}
	}
}
